<!--
Copyright 2010, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
    * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>
		Javascript Audio Processing
	</title>
	<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?autoload=true&amp;skin=sunburst&amp;lang=css" defer="defer"></script>
	<!-- show all line numbers-->
	<style type="text/css">
		body {
			font-family:Arial, Helvetica, sans-serif;
		}
		.prettyprint ol.linenums>li {
			list-style-type: decimal
		}
	</style>
	<script src="../build/maximilian.js"></script>
</head>

<body>
	<h1> FFT </h1>
	<p>
		The Fast Fourrier Transform analysis.
	</p>
	<button id="playButton">Play</button>
	<!-- <canvas id="myCanvas" width="700" height="700" style="border:1px solid #c3c3c3;">
	</canvas> -->
</body>

<script type="text/javascript">
  
  let m = maximilian();

  let fftSize = 1024;
  let magMult = 6;
  let wave = 0;
 
  let playAudio = () => {

    let myOsc = new m.maxiOsc();
    let myOsc2 = new m.maxiOsc();
    let maxiAudio = new m.maxiAudio();
    let samplePlayer = new m.maxiSample();
    let fft = new m.maxiFFT();
    let magnitudes = new m.VectorFloat();
    let mags = new Float32Array();
  	
    maxiAudio.init();
    maxiAudio.loadSample("audio/beat2.wav", samplePlayer);	
    fft.setup(fftSize, 512, 256);

    maxiAudio.play = function () {

      wave = samplePlayer.play();
      
      if (fft.process(wave, 0)) {
        magnitudes = fft.getMagnitudesDB();
        console.log(magnitudes);        
        for (let i = 0; i < fftSize / 2; i++){
          mags[i] = magnitudes.get(i);
        }
        console.log(mags); 
      }
      return wave;
    }
  }

function draw() {
  context.clearRect(0, 0, 700, 700);
  context.fillStyle = "#FF0000";
  context.font = "30px Arial";
  context.fillText("FFT", 70, 200);
  for (let i = 0; i < fftSize / 2; i++) {
    context.beginPath();
    context.rect(
    fftDraw.barsLeft + i,
    fftDraw.barsBottom,
    fftDraw.barsSize,
    -(fft.getMagnitude(i) * fftDraw.magMult));
    context.fill();
    context.closePath();
  }
}

  const playButton = document.getElementById('playButton');
  playButton.addEventListener("click", () => playAudio());

  window.onload = function setup() {
    let canvas = document.getElementById("myCanvas");
    context = canvas.getContext("2d");
    // return setInterval(draw, 40);
  }

</script>

<pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c">

  let m = maximilian();

  let fftSize = 1024;
  let magMult = 6;
  let wave = 0;
 
  let playAudio = () => {

    let myOsc = new m.maxiOsc();
    let myOsc2 = new m.maxiOsc();
    let maxiAudio = new m.maxiAudio();
    let samplePlayer = new m.maxiSample();
    let fft = new m.maxiFFT();
    let magnitudes = new m.VectorFloat();
    let mags = new Float32Array();
  	
    maxiAudio.init();
    maxiAudio.loadSample("audio/beat2.wav", samplePlayer);	
    fft.setup(fftSize, 512, 256);
     
    maxiAudio.play = function () {

      wave = samplePlayer.play();
      
      if (fft.process(wave, 0)) {
        magnitudes = fft.getMagnitudesDB();
        console.log(magnitudes);        
        for (let i = 0; i < fftSize / 2; i++){
          mags[i] = magnitudes.get(i);
        }
        console.log(mags); 
      }
      return wave;
    }
  }

  function draw() {
    context.clearRect(0, 0, 700, 700);
    context.fillStyle = "#FF0000";
    context.font = "30px Arial";
    context.fillText("FFT", 70, 200);
    for (let i = 0; i < fftSize / 2; i++) {
      context.beginPath();
      context.rect(
      fftDraw.barsLeft + i,
      fftDraw.barsBottom,
      fftDraw.barsSize,
      -(fft.getMagnitude(i) * fftDraw.magMult));
      context.fill();
      context.closePath();
    }
  }

  const playButton = document.getElementById('playButton');
  playButton.addEventListener("click", () => playAudio());

  window.onload = function setup() {
    let canvas = document.getElementById("myCanvas");
    context = canvas.getContext("2d");
    // return setInterval(draw, 40);
  }

</pre>

</html>